package com.douqu.game.core.config.major;

import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.entity.GameObject;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author wangzhenfei
 *         2017-11-28 16:03
 *         专业加技能
 */
public class MajorMergeConfig extends GameObject {
    /**技能类型**/
    public int majorType;
    /**原始材料id**/
    public List<Integer> srcMaterial;
    /**目标材料**/
    public GoodsData[] desMaterial;
    public int desMaterialWeight;
    /**需要技能等级**/
    public int needSkillLv;


    @Override
    public boolean check()
    {
        if(srcMaterial == null)
        {
            LogUtils.error("MajorConfig check srcMaterial is null -> id:" + id + " name:" + name);
        }

        if(desMaterial == null)
        {
            LogUtils.error("MajorConfig check desMaterial is null -> id:" + id + " name:" + name);
        }


        SGCommonProto.E_MAJOR_TYPE type = SGCommonProto.E_MAJOR_TYPE.forNumber(majorType);
        if(type == null || type == SGCommonProto.E_MAJOR_TYPE.MAJOR_TYPE_UNKNOW)
        {
            LogUtils.error("MajorConfig check desMaterial is null -> id:" + id + " name:" + name);
        }
        return true;
    }


    @Override
    public boolean setVariable(String key, String value)
    {
        if("srcMaterial".equals(key)){
            srcMaterial = new ArrayList<>();
            int [] ss = LoadUtils.loadIntArray(value);
            for(int temp : ss){
                if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.PROP_KEY, temp) == null){
                    LogUtils.error("道具找不到：" + temp);
                    return false;
                }else {
                    srcMaterial.add(temp);
                }
            }
            return true;
        }else if("desMaterial".equals(key))
        {
            desMaterialWeight = 0;
            desMaterial = LoadUtils.loadGoodDataToArray(key, value);
            for(GoodsData data : desMaterial){
                desMaterialWeight += data.value;
            }
            return LoadUtils.checkGoods(desMaterial, this.getClass(), key, id);
        }
        else
            return super.setVariable(key, value);
    }

    @Override
    public String toString() {
        return "MajorMergeConfig{" +
                "majorType=" + majorType +
                ", srcMaterial=" + srcMaterial +
                ", desMaterial=" + Arrays.toString(desMaterial) +
                ", desMaterialWeight=" + desMaterialWeight +
                ", needSkillLv=" + needSkillLv +
                "} " + super.toString();
    }
}
